package server

import (
	"JudgeClient/global"
	"JudgeClient/utils"
	"github.com/gin-gonic/gin"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/mem"
	"net/http"
	"os"
	"runtime"
)

func Ping(c *gin.Context) {

	if !utils.CheckToken(c.GetHeader("token")) {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"data": "wrong token",
		})
		return
	}

	hostname, err := os.Hostname()

	if err != nil {
		hostname = global.Conf.Server.Host
	}

	cpuPercent, _ := cpu.Percent(0, false)
	vMem, _ := mem.VirtualMemory()

	c.JSON(http.StatusOK, gin.H{
		"judge_cli_version": "0.1.0",
		"hostname":          hostname,
		"cpu_core":          runtime.NumCPU(),
		"cpu":               cpuPercent,
		"memory":            vMem.UsedPercent,
		"action":            "pong",
	})
}
